<?php

namespace StarCloud\Library\Sdk;

use Framework\DiServices\HttpClient;
use GuzzleHttp\Client;
use Phalcon\Di;
use StarCloud\Library\HttpCurl;

class Wangpan
{
    /* @var $httpClient HttpClient */
    private $httpClient = null;

    public function __construct()
    {
        $this->httpClient = Di::getDefault()->get('httpClient');
        $this->cacheClass = Di::getDefault()->get('redis');
    }

    public function getHeader()
    {
        return [
            'Content-Type' => 'application/json',
            'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
        ];
    }
    public function getCategory()
    {
        $url = 'https://api.hunhepan.com/v1/extab/list_all';
        $response = $this->httpClient->sendGetRequest($url, [], $this->getHeader());
        $json = json_decode($response->getBody()->getContents(), true);

        if (isset($json['code']) && $json['code'] == 200) {
            return $json['data'];
        }

        return false;
    }

    public function getListByCategoryId($categoryId, $page = 1, $size = 20)
    {
        $url = "https://api.hunhepan.com/v1/extab/raw_disks/{$categoryId}?page={$page}&size={$size}&enabled=1";
        $response = $this->httpClient->sendGetRequest($url, [], $this->getHeader());
        $json = json_decode($response->getBody()->getContents(), true);

        if (isset($json['code']) && $json['code'] == 200) {
            return $this->formatList($json['data']);
        }

        return [];
    }

    public function searchList($keyword, $page = 1, $size = 20)
    {
        $url = "https://api.hunhepan.com/open/search/disk";
        $body = [
            "q" => $keyword,
            "page" => intval($page),
            "size" => intval($size),
            "time" => "month",
//            "type" => "",
            "exact" => true,
            'enabled' => 1,
        ];
        $response = $this->httpClient->sendPostRequest($url, $body, $this->getHeader());

        $json = json_decode($response->getBody()->getContents(), true);

        if (isset($json['code']) && $json['code'] == 200) {
            return $this->formatList($json['data']);
        }

        return [];

    }

    public function formatList($data)
    {
        $list = [];
        foreach ($data['list'] as $item) {
            if (isset($item['files']) && is_string($item['files'])) {
                $item['files'] = [$item['files']];
            }
            $item['type_icon'] = self::disk_icon[$item['disk_type']]['icon'] ?? '';
            $item['type_name'] = self::disk_icon[$item['disk_type']]['name'] ?? '其他';
            if (!isset($item['enabled'])) {
                $item['enabled'] = true;
            }
            if ($item['shared_time'] != $item['update_time']) {
                $list[] = $item;
            }
        }
        $data['list'] = $list;

        return $data;
    }

    const disk_icon = [
        'UC' => [
            'name' => 'UC网盘',
            'icon' => 'https://cdn.doumvip.com/fd2ebe80654d64aa8cdf47558313caa3.png'
        ],
        'QUARK' => [
            'name' => '夸克网盘',
            'icon' => 'https://cdn.doumvip.com/4449e1f5d684a8c7dfdec6427d77fd54.png'
        ],
        'BDY' => [
            'name' => '百度网盘',
            'icon' => 'https://cdn.doumvip.com/fbb4652fbbd866f34fd449b5b4562687.png'
        ],
        'XUNLEI' => [
            'name' => '迅雷网盘',
            'icon' => 'https://cdn.doumvip.com/ba928a20c0781b9d6a0b128b23dd904d.png'
        ],
    ];

    public function search($keyword, $page = 1, $size = 20)
    {

    }
}

